package stanford.com.balldating;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cn.bmob.v3.Bmob;
import cz.msebera.android.httpclient.Header;
import stanford.com.balldating.chat.ChatService;
import stanford.com.balldating.util.HttpClientUtils;
import stanford.com.balldating.util.UserUtils;

public class MainActivity extends AppCompatActivity {

    EditText ediyonghuming,edimima;
    TextView txvwarning;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this, "87e563b7c9641d8d10cb9990943a4009");
        ediyonghuming=(EditText)findViewById(R.id.ediyonghuming);
        edimima=(EditText)findViewById(R.id.edimima);
        txvwarning=(TextView) findViewById(R.id.txvwarning);
    }

    public void gotozhucejiemian(View view)
    {
        Intent it=new Intent(this,ZhuceActivity.class);
        startActivity(it);
    }



    public void gotomain2activity(View view)
    {
        if(ediyonghuming.getText().toString().length()==0||edimima.getText().toString().
                length()==0)
        {
            txvwarning.setText("用户名或密码为空！");
        }
        else{

            final String userName=ediyonghuming.getText().toString();
            String password=edimima.getText().toString();
            RequestParams params=new RequestParams();
            params.add("username",userName);
            params.add("password",password);
            HttpClientUtils.post("login", params, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    try {
                        JSONObject json = new JSONObject(responseString);
                        boolean success=json.getBoolean("success");
                        if(success){
                            User user=new User();
                            user.setname(userName);
                            UserUtils.setUser(user);
                            Intent seccess = new Intent();

                            Intent intent = new Intent(MainActivity.this, ChatService.class);
                            startService(intent);

                            seccess.setClass(MainActivity.this, Main2Activity.class);
                            startActivity(seccess);
                        }else{
                            txvwarning.setText("");
                            Toast.makeText(MainActivity.this, "帐号或密码有误", Toast.LENGTH_LONG).show();
                        }
                    }catch (JSONException exception){
                    }
                }
            });
            /*
            final BmobUser user=new BmobUser();
            user.setUsername(userName);
            user.setPassword(password);
            BmobQuery<User> query=new BmobQuery<User>();
            query.addWhereEqualTo("name", userName);
            query.addWhereEqualTo("password", password);
            query.findObjects(new FindListener<User>() {

                @Override
                public void done(List<User> arg0, BmobException e) {
                    // TODO Auto-generated method stub
                    if(e==null){
                        String gname=arg0.get(0).getname().toString();
                        String gpassword=arg0.get(0).getPassword().toString();
                        String name=ediyonghuming.getText().toString();
                        String password=edimima.getText().toString();
                        txvwarning.setText("");
                        Toast.makeText(MainActivity.this, gname, Toast.LENGTH_LONG).show();
                        if(gname.equals(name)&&gpassword.equals(password))
                        {
                            Intent seccess = new Intent();
                            seccess.setClass(MainActivity.this, Main2Activity.class);
                            startActivity(seccess);
                        }

                    }
                    else{
                        txvwarning.setText("");
                        Toast.makeText(MainActivity.this, "帐号或密码有误", Toast.LENGTH_LONG).show();
                    }

                }
            });*/


        }
    }
}

